package com.spic.business.riskbase.hierarchy.service.impl;

import com.spic.business.riskbase.domain.RiskTemp;
import com.spic.business.riskbase.hierarchy.domain.*;
import com.spic.business.riskbase.hierarchy.service.*;
import com.spic.business.riskbase.service.IRiskTempService;
import com.spic.common.core.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : 赵祥
 * create at:  2023/4/15  16:54
 * @Description :
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class DealDataServiceImpl implements IDealDataService {
    private static final Logger logger = LoggerFactory.getLogger(DealDataServiceImpl.class);
    @Autowired
    private IRiskWorkTaskService riskWorkTaskService;
    @Autowired
    private IRiskTempService riskTempService;
    @Autowired
    private IRiskWorkStepService workStepService;
    @Autowired
    private IRiskDangerSourceService riskDangerSourceService;
    @Autowired
    private IRiskDescriptionService riskDescriptionService;
    @Autowired
    private IRiskAnalyseService riskAnalyseService;
    @Autowired
    private IRiskDetailsInfoService riskDetailsInfoService;

    @Override
    public void insertWorkStep(RiskWorkTask riskWorkTask) {
        Long parentDeptId = riskWorkTask.getParentDeptId();
        if (parentDeptId == null) {
            throw new ServiceException("部门id不能为空");
        }
        List<RiskWorkTask> riskWorkTasks = riskWorkTaskService.selectRiskWorkTaskList(riskWorkTask);
        for (int i = 0; i < riskWorkTasks.size(); i++) {
            RiskWorkTask workTask = riskWorkTasks.get(i);
            //工作任务id
            Long id = workTask.getId();
            String workTaskName = workTask.getWorkTaskName();
            //根据工作任务查询工作步骤
            RiskTemp riskTemp = new RiskTemp();
            riskTemp.setDeptId(parentDeptId.toString());
            riskTemp.setWorkTask(workTaskName);
            List<String> riskTemps = riskTempService.selectWorkStepByWorkTask(riskTemp);

            List<RiskWorkStep> list = new ArrayList<>();
            for (int j = 0; j < riskTemps.size(); j++) {
                RiskWorkStep riskWorkStep = new RiskWorkStep();
                riskWorkStep.setWorkTaskId(id);
                riskWorkStep.setWorkTaskName(workTaskName);
                riskWorkStep.setWorkStepName(riskTemps.get(j));
                riskWorkStep.setParentDeptId(parentDeptId);
                riskWorkStep.setOrderBy(j + 1);
                list.add(riskWorkStep);
            }
            //先删除
            int i1 = workStepService.deleteRiskWorkStepByWorkTaskId(id);
            //再插入
            int i2 = workStepService.batchRiskWorkStep(list);
            logger.info("工作任务{} 处理工作步骤,删除条数:{}, 插入成功条数:{}", workTaskName, i1, i2);
        }
    }

    @Override
    public void inserDangerSource(RiskWorkStep riskWorkStep) {
        Long parentDeptId = riskWorkStep.getParentDeptId();
        StringBuilder sb = new StringBuilder();
        if (parentDeptId == null) {
            throw new ServiceException("部门id不能为空");
        }
        List<RiskWorkStep> riskWorkSteps = workStepService.selectRiskWorkStepList(riskWorkStep);
        for (int i = 0; i < riskWorkSteps.size(); i++) {
            RiskWorkStep workStep = riskWorkSteps.get(i);
            //根据作业步骤查询危险源
            RiskTemp riskTemp = new RiskTemp();
            riskTemp.setDeptId(parentDeptId.toString());
            riskTemp.setWorkTask(workStep.getWorkTaskName());
            riskTemp.setWorkStep(workStep.getWorkStepName());
            List<String> riskTemps = riskTempService.selectDangerSourceByWorkStep(riskTemp);

            List<RiskDangerSource> list = new ArrayList<>();
            for (int j = 0; j < riskTemps.size(); j++) {
                RiskDangerSource dangerSource = new RiskDangerSource();
                dangerSource.setParentDeptId(parentDeptId);
                dangerSource.setWorkTaskId(workStep.getWorkTaskId());
                dangerSource.setWorkTaskName(workStep.getWorkTaskName());
                dangerSource.setWorkStepId(workStep.getId());
                dangerSource.setWorkStepName(workStep.getWorkStepName());
                dangerSource.setDangerSourceName(riskTemps.get(j));
                list.add(dangerSource);
            }
            int i1 = riskDangerSourceService.deleteDangerSourceByWorkStepId(workStep.getId());
            int i2 = riskDangerSourceService.batchRiskDangerSource(list);
            sb.append("作业步骤: " + workStep.getWorkStepName() + ":删除条数:" + i1 + ",新增条数:" + i2 + "\n");
        }
        logger.info(sb.toString());
    }

    @Override
    public void inserRiskDescription(RiskDangerSource riskDangerSource) {
        Long parentDeptId = riskDangerSource.getParentDeptId();
        StringBuilder sb = new StringBuilder();
        if (parentDeptId == null) {
            throw new ServiceException("部门id不能为空");
        }
        //查询该部门下的所有危险源
        List<RiskDangerSource> riskDangerSources = riskDangerSourceService.selectRiskDangerSourceList(riskDangerSource);
        //迭代处理每个危险源下的风险描述
        for (int i = 0; i < riskDangerSources.size(); i++) {
            RiskDangerSource dangerSource = riskDangerSources.get(i);
            //查询临时表,查询危险源对应的所有风险描述
            RiskTemp riskTemp = new RiskTemp();
            riskTemp.setDeptId(parentDeptId.toString());
            riskTemp.setWorkTask(dangerSource.getWorkTaskName());
            riskTemp.setWorkStep(dangerSource.getWorkStepName());
            riskTemp.setDangerSource(dangerSource.getDangerSourceName());
            List<String> riskTemps = riskTempService.selectDangerDescByDangerSource(riskTemp);
            List<RiskDescription> list = new ArrayList<>();
            for (int j = 0; j < riskTemps.size(); j++) {
                RiskDescription riskDescription = new RiskDescription();
                riskDescription.setParentDeptId(parentDeptId);
                riskDescription.setWorkTaskId(dangerSource.getWorkTaskId());
                riskDescription.setWorkTaskName(dangerSource.getWorkTaskName());
                riskDescription.setWorkStepId(dangerSource.getWorkStepId());
                riskDescription.setWorkStepName(dangerSource.getWorkStepName());
                riskDescription.setDangerSourceId(dangerSource.getId());
                riskDescription.setDangerSourceName(dangerSource.getDangerSourceName());
                riskDescription.setRiskDescriptionName(riskTemps.get(j));
                list.add(riskDescription);
            }
            int i1 = riskDescriptionService.deleteRiskDescriptionByDangerSource(dangerSource.getId());
            int i2 = riskDescriptionService.batchRiskDescription(list);
            sb.append("危险源: " + dangerSource.getDangerSourceName() + ":删除风险描述条数:" + i1 + ",新增风险描述条数:" + i2 + "\n");
        }
        logger.info("\n" + sb);
    }

    @Override
    public void inserRiskAnalyse(RiskDescription riskDescription) {
        Long parentDeptId = riskDescription.getParentDeptId();
        StringBuilder sb = new StringBuilder();
        if (parentDeptId == null) {
            throw new ServiceException("部门id不能为空");
        }
        //查询该部门下的所有风险描述
        List<RiskDescription> riskDescriptions = riskDescriptionService.selectRiskDescriptionList(riskDescription);
        //迭代处理每个风险描述下的风险分析
        for (int i = 0; i < riskDescriptions.size(); i++) {
            RiskDescription description = riskDescriptions.get(i);
            //查询临时表,查询风险描述对应的所有风险分析
            RiskTemp riskTemp = new RiskTemp();
            riskTemp.setDeptId(parentDeptId.toString());
            riskTemp.setWorkTask(description.getWorkTaskName());
            riskTemp.setWorkStep(description.getWorkStepName());
            riskTemp.setDangerSource(description.getDangerSourceName());
            riskTemp.setRiskDescription(description.getRiskDescriptionName());
            List<String> riskTemps = riskTempService.selectRiskAnalyseByDangerDesc(riskTemp);

            List<RiskAnalyse> list = new ArrayList<>();
            for (int j = 0; j < riskTemps.size(); j++) {
                RiskAnalyse riskAnalyse = new RiskAnalyse();
                riskAnalyse.setParentDeptId(parentDeptId);
                riskAnalyse.setWorkTaskId(description.getWorkTaskId());
                riskAnalyse.setWorkTaskName(description.getWorkTaskName());
                riskAnalyse.setWorkStepId(description.getWorkStepId());
                riskAnalyse.setWorkStepName(description.getWorkStepName());
                riskAnalyse.setDangerSourceId(description.getId());
                riskAnalyse.setDangerSourceName(description.getDangerSourceName());
                riskAnalyse.setRiskDescriptionId(description.getId());
                riskAnalyse.setRiskDescriptionName(description.getRiskDescriptionName());
                riskAnalyse.setRiskAnalyseName(riskTemps.get(j));
                list.add(riskAnalyse);
            }
            int i1 = riskAnalyseService.deleteRiskAnalyseByRiskDescription(description.getId());
            int i2 = riskAnalyseService.batchRiskAnalyse(list);
            sb.append("风险描述: " + description.getRiskDescriptionName() + ":删除风险分析条数:" + i1 + ",新增风险分析条数:" + i2 + "\n");
        }
        logger.info("\n" + sb);
    }

    @Override
    public void inserRiskDetails(RiskAnalyse riskAnalyse) {
        Long parentDeptId = riskAnalyse.getParentDeptId();
        StringBuilder sb = new StringBuilder();
        if (parentDeptId == null) {
            throw new ServiceException("部门id不能为空");
        }
        //查询该部门下的所有风险分析
        List<RiskAnalyse> riskAnalyses = riskAnalyseService.selectRiskAnalyseList(riskAnalyse);
        //迭代处理每个风险分析下的风险详情
        for (int i = 0; i < riskAnalyses.size(); i++) {
            RiskAnalyse analyse = riskAnalyses.get(i);
            //查询临时表,查询每个风险分析对应的详情信息
            RiskTemp riskTemp = new RiskTemp();
            riskTemp.setDeptId(parentDeptId.toString());
            riskTemp.setWorkTask(analyse.getWorkTaskName());
            riskTemp.setWorkStep(analyse.getWorkStepName());
            riskTemp.setDangerSource(analyse.getDangerSourceName());
            riskTemp.setRiskDescription(analyse.getRiskDescriptionName());
            riskTemp.setRiskAnalyse(analyse.getRiskAnalyseName());
            List<RiskTemp> riskTemps = riskTempService.selectRiskTempList(riskTemp);

            List<RiskDetailsInfo> list = new ArrayList<>();
            for (int j = 0; j < riskTemps.size(); j++) {
                RiskTemp temp = riskTemps.get(j);
                RiskDetailsInfo riskDetailsInfo = new RiskDetailsInfo();
                riskDetailsInfo.setParentDeptId(parentDeptId);
                riskDetailsInfo.setWorkTaskId(analyse.getWorkTaskId());
                riskDetailsInfo.setWorkTaskName(analyse.getWorkTaskName());
                riskDetailsInfo.setWorkStepId(analyse.getWorkStepId());
                riskDetailsInfo.setWorkStepName(analyse.getWorkStepName());
                riskDetailsInfo.setDangerSourceId(analyse.getId());
                riskDetailsInfo.setDangerSourceName(analyse.getDangerSourceName());
                riskDetailsInfo.setRiskDescriptionId(analyse.getId());
                riskDetailsInfo.setRiskDescriptionName(analyse.getRiskDescriptionName());
                riskDetailsInfo.setRiskAnalyseId(analyse.getId());
                riskDetailsInfo.setRiskAnalyseName(analyse.getRiskAnalyseName());
                riskDetailsInfo.setRiskResultAffect(temp.getRiskResultAffect());
                riskDetailsInfo.setRiskL(temp.getRiskL());
                riskDetailsInfo.setRiskE(temp.getRiskE());
                riskDetailsInfo.setRiskC(temp.getRiskC());
                riskDetailsInfo.setRiskD(temp.getRiskD());
                riskDetailsInfo.setRiskGrade(temp.getRiskGrade());
                riskDetailsInfo.setControlMeasure(temp.getControlMeasure());
                riskDetailsInfo.setRegulationInfo(temp.getRegulationInfo());
                list.add(riskDetailsInfo);
            }
            int i1 = riskDetailsInfoService.deleteRiskDetailsByRiskAnalyse(analyse.getId());
            int i2 = riskDetailsInfoService.batchRiskDetailsInfo(list);
            sb.append("风险分析: " + analyse.getRiskAnalyseName() + ":删除风险分析详情条数:" + i1 + ",新增风险分析详情条数:" + i2 + "\n");
        }
        logger.info("\n" + sb);
    }
}
